//#region usernameAndOthername
var SENDDATA = {}
//#endregion

//#region websocket函数
function Websocket(){
    //创建websocket server
    var socket = new WebSocket('ws://localhost:6060');

    // socket.onmessage = function(event){
    //     console.log('收到消息：', event.data);
    // }
    
    //监听连接
    socket.onopen = function(){
        console.log('websocket已连接');
    }

    //监听关闭
    socket.onclose = function(){
        console.log('websocket已关闭');
    }

    //监听错误
    socket.onerror = function(error){
        console.log('websocket发生错误：', error);
    }

    return socket
}
//#endregion

//#region 登录的接口
/**
 * 登录的接口
 */
async function sendMsg(msg,callback){
    //fetch api请求
    let soket = null;
    let data = await fetch("http://localhost:6061/login",{
        method:"post",
        headers:{
            "Content-Type":"application/json"
        },
        body:JSON.stringify(msg),
    })
    if(data.statusText == "OK"){
        //await 有两个作用 一个是异步 另一个原因是返回promise 的返回值 （个人理解）
        soket = Websocket()
        soket.onopen = function(){
            SENDDATA = msg; //将登录信息发送给websocket
            console.log('websocket已连接');
            callback(soket)
        }
    }
}
//#endregion